package com.rmt.www.serviceImpl;

import com.opensymphony.xwork2.ActionContext;
import com.rmt.www.constant.ArtifactStatus;
import com.rmt.www.dao.ProjectDao;
import com.rmt.www.dao.RequirementArtifactDao;
import com.rmt.www.dao.RequirementArtifactHistoryDao;
import com.rmt.www.dao.RequirementArtifactRequestDao;
import com.rmt.www.dao.UserDao;
import com.rmt.www.service.SREManageRequirementService;
import com.rmt.www.utility.SystemTime;
import com.rmt.www.vo.RequirementArtifact;
import com.rmt.www.vo.RequirementArtifactHistory;
import com.rmt.www.vo.RequirementArtifactRequest;
import com.rmt.www.vo.User;

public class SREManageRequirementServiceImpl implements
		SREManageRequirementService {

	private RequirementArtifactDao requirementArtifactDao;
	private RequirementArtifactRequestDao requirementArtifactRequestDao;
	private RequirementArtifactHistoryDao requirementArtifactHistoryDao;
	private UserDao userDao;
	private ProjectDao projectDao;
	
	public ProjectDao getProjectDao() {
		return projectDao;
	}

	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	public RequirementArtifactDao getRequirementArtifactDao() {
		return requirementArtifactDao;
	}

	public void setRequirementArtifactDao(
			RequirementArtifactDao requirementArtifactDao) {
		this.requirementArtifactDao = requirementArtifactDao;
	}

	public RequirementArtifactRequestDao getRequirementArtifactRequestDao() {
		return requirementArtifactRequestDao;
	}

	public void setRequirementArtifactRequestDao(
			RequirementArtifactRequestDao requirementArtifactRequestDao) {
		this.requirementArtifactRequestDao = requirementArtifactRequestDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	
	public void addNewRequirement(RequirementArtifactRequest r) {
		boolean isprojectbaselinecreated = projectDao.getProject(r.getPid()).isIsbaselinecreated();
		if (isprojectbaselinecreated)
		{
			requirementArtifactRequestDao.insertRequirementArtifactRequest(r);
		}
		else 
		{
			RequirementArtifact ra = new RequirementArtifact();
			ra.setPid(r.getPid());
			ra.setTitle(r.getTitle());
			ra.setVersion(r.getVersion());
			ra.setPriority(r.getPriority());
			ra.setSource(r.getSource());
			ra.setRational(r.getRational());
			ra.setStatus(r.getStatus());
			ra.setDescription(r.getDescription());
			ra.setCost(r.getCost());
			ra.setEffort(r.getEffort());
			ra.setRisk(r.getRisk());
			ra.setUpdatedtimes(r.getUpdatedtimes());
			ra.setRequirementtype(r.getRequirementtype());
			ra.setAuthorId(r.getAuthorId());
			ra.setUpdateUserId(r.getUpdateUserId());
			ra.setCreatedtime(r.getCreatedtime());
			ra.setUpdatedtime(r.getUpdatedtime());
			
			int lastinsertid = requirementArtifactDao.insertRequirementArtifact(ra);
			
			RequirementArtifactHistory h = new RequirementArtifactHistory();
			h.setPid(r.getPid());
			h.setTitle(r.getTitle());
			h.setVersion(r.getVersion());
			h.setPriority(r.getPriority());
			h.setSource(r.getSource());
			h.setRational(r.getRational());
			h.setStatus(r.getStatus());
			h.setDescription(r.getDescription());
			h.setCost(r.getCost());
			h.setEffort(r.getEffort());
			h.setRisk(r.getRisk());
			h.setUpdatedtimes(r.getUpdatedtimes());
			h.setRequirementtype(r.getRequirementtype());
			h.setAuthorId(r.getAuthorId());
			h.setUpdateUserId(r.getUpdateUserId());
			h.setCreatedtime(r.getCreatedtime());
			h.setUpdatedtime(r.getUpdatedtime());
			h.setRequirementartifactid(lastinsertid);
			
			requirementArtifactHistoryDao.insertRequirementArtifactHistory(h);
		}
	}

	public void deleteRequirement(int id) {
		RequirementArtifact ra=requirementArtifactDao.getRequirementArtifactById(id);
		boolean isprojectbaselinecreated = projectDao.getProject(ra.getPid()).isIsbaselinecreated();
		if (isprojectbaselinecreated)
		{
			RequirementArtifactRequest raq=new RequirementArtifactRequest();
			raq.setPid(ra.getPid());
			raq.setTitle(ra.getTitle());
			raq.setAuthorId(ra.getAuthorId());
			raq.setVersion(ra.getVersion());
			raq.setPriority(ra.getPriority());
			raq.setSource(ra.getSource());
			raq.setRational(ra.getRational());
			raq.setCreatedtime(ra.getCreatedtime());
			
			User u=(User)ActionContext.getContext().getSession().get("currentUser");
		 	raq.setUpdateUserId(u.getUid());
		 	//还要设置updatedtime为当前时间
		 	raq.setUpdatedtime(new java.sql.Timestamp(SystemTime.getSystemTime().getTime()));
		 	
		 	raq.setStatus(ra.getStatus());
		 	raq.setDescription(ra.getDescription());
		 	raq.setCost(ra.getCost());
		 	raq.setEffort(ra.getEffort());
		 	raq.setRisk(ra.getRisk());
		 	raq.setUpdatedtimes(ra.getUpdatedtimes());
		 	raq.setRequirementtype(ra.getRequirementtype());
		 	raq.setRequirementartifactid(id);
		 	raq.setRequesttype("delete");
		 	
		 	requirementArtifactRequestDao.insertRequirementArtifactRequest(raq);
		}
		else 
		{
			RequirementArtifactHistory h = new RequirementArtifactHistory();
			h.setPid(ra.getPid());
			h.setTitle(ra.getTitle());
			h.setVersion(ra.getVersion());
			h.setPriority(ra.getPriority());
			h.setSource(ra.getSource());
			h.setRational(ra.getRational());
			h.setStatus(ArtifactStatus.DELETED);		//mark as deleted
			h.setDescription(ra.getDescription());
			h.setCost(ra.getCost());
			h.setEffort(ra.getEffort());
			h.setRisk(ra.getRisk());
			h.setUpdatedtimes(ra.getUpdatedtimes());
			h.setRequirementtype(ra.getRequirementtype());
			h.setAuthorId(ra.getAuthorId());
			h.setUpdateUserId(ra.getUpdateUserId());
			h.setCreatedtime(ra.getCreatedtime());
			h.setUpdatedtime(ra.getUpdatedtime());
			h.setRequirementartifactid(ra.getId());
			
			requirementArtifactHistoryDao.insertRequirementArtifactHistory(h);
			requirementArtifactDao.deleteRequirementArtifactById(id);
		}
	}

	public void updateRequirement(RequirementArtifactRequest r) {
		boolean isprojectbaselinecreated = projectDao.getProject(r.getPid()).isIsbaselinecreated();
		if (isprojectbaselinecreated)
		{
			requirementArtifactRequestDao.insertRequirementArtifactRequest(r);
		}
		else 
		{
			RequirementArtifact ra = new RequirementArtifact();
			ra.setId(r.getRequirementartifactid());//注意这里，是后来新增的
			ra.setPid(r.getPid());
			ra.setTitle(r.getTitle());
			ra.setVersion(r.getVersion());
			ra.setPriority(r.getPriority());
			ra.setSource(r.getSource());
			ra.setRational(r.getRational());
			ra.setStatus(r.getStatus());
			ra.setDescription(r.getDescription());
			ra.setCost(r.getCost());
			ra.setEffort(r.getEffort());
			ra.setRisk(r.getRisk());
			ra.setUpdatedtimes(r.getUpdatedtimes());
			ra.setRequirementtype(r.getRequirementtype());
			ra.setAuthorId(r.getAuthorId());
			ra.setUpdateUserId(r.getUpdateUserId());
			ra.setCreatedtime(r.getCreatedtime());
			ra.setUpdatedtime(r.getUpdatedtime());
			
			requirementArtifactDao.updateRequirementArtifact(ra);
			
			RequirementArtifactHistory h = new RequirementArtifactHistory();
			h.setPid(r.getPid());
			h.setTitle(r.getTitle());
			h.setVersion(r.getVersion());
			h.setPriority(r.getPriority());
			h.setSource(r.getSource());
			h.setRational(r.getRational());
			h.setStatus(r.getStatus());
			h.setDescription(r.getDescription());
			h.setCost(r.getCost());
			h.setEffort(r.getEffort());
			h.setRisk(r.getRisk());
			h.setUpdatedtimes(r.getUpdatedtimes());
			h.setRequirementtype(r.getRequirementtype());
			h.setAuthorId(r.getAuthorId());
			h.setUpdateUserId(r.getUpdateUserId());
			h.setCreatedtime(r.getCreatedtime());
			h.setUpdatedtime(r.getUpdatedtime());
			h.setRequirementartifactid(r.getRequirementartifactid());
			
			requirementArtifactHistoryDao.insertRequirementArtifactHistory(h);
		}
	}

	public RequirementArtifact getRequirementArtifactById(int id) {
		RequirementArtifact ra=requirementArtifactDao.getRequirementArtifactById(id);
		String authorName=userDao.getUser(ra.getAuthorId()).getName();
		ra.set_authorname(authorName);
		return ra;
	}

	public RequirementArtifactHistoryDao getRequirementArtifactHistoryDao() {
		return requirementArtifactHistoryDao;
	}

	public void setRequirementArtifactHistoryDao(
			RequirementArtifactHistoryDao requirementArtifactHistoryDao) {
		this.requirementArtifactHistoryDao = requirementArtifactHistoryDao;
	}

}
